//
//  ServiceManager.m
//  insyte
//
//  Created by Vu Dang Tuan on 6/21/15.
//  Copyright (c) 2015 Robert. All rights reserved.
//

#import "ServiceManager.h"
#import "MBProgressHUD.h"
#import "Constants.h"
#import "AFHTTPRequestOperationManager.h"
#import "Helpers.h"
#import "GlobalObject.h"
#import "DBManager.h"

@implementation ServiceManager
+ (void)loginWithPhone:(NSString *)code phone:(NSString *)phone cb:(void (^)(BOOL))completion{
    
    UIView * currentView = [[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];
    [MBProgressHUD showHUDAddedTo:currentView animated:YES];
    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        NSString *loginPhone = [SERVER_ADDRESS stringByAppendingString:LOGIN_WITH_PHONE];
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        NSDictionary *params = @{@"phone": phone,
                                 @"verify_code": code
                                 };
        [manager POST:loginPhone parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [MBProgressHUD hideHUDForView:currentView animated:NO];
                NSLog(@"respone: %@", responseObject);
                int success = [responseObject[@"code"] intValue];
                if (success == CODE_SUCCESS) {
                    // return value
                    if (completion) {
                        // save data
                        NSDictionary *dict = (NSDictionary*) responseObject[@"data"];
                        BOOL success = NO;
                        success = [[DBManager getSharedInstance] saveUser:[Helpers parseUserObject:dict]];
                        if (success == NO) {
                            completion(FALSE);
                        }else{
                            completion(TRUE);
                        }
                    }
                }else{
                    if (completion) {
                        completion(FALSE);
                    }
                    [Helpers showAnnouncementDialog:(NSString *)responseObject[@"message"]];
                }
            });
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"%@",error);
                [MBProgressHUD hideHUDForView:currentView animated:YES];
                [Helpers showAnnouncementDialog:CONNECTION_FAILD_LABEL];
            });
        }];
    });
}
+(void)loginWithEmail:(NSString *)email password:(NSString *)password cb:(void (^)(BOOL))completion
{
    UIView * currentView = [[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];
    [MBProgressHUD showHUDAddedTo:currentView animated:YES];
    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        NSString *loginURL = [SERVER_ADDRESS stringByAppendingString:LOGIN_WITH_EMAIL];
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        NSDictionary *params = @{@"email": email,
                                 @"password": password};
        [manager POST:loginURL parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [MBProgressHUD hideHUDForView:currentView animated:NO];
                NSLog(@"respone: %@", responseObject);
                int success = [responseObject[@"code"] intValue];
                if (success == CODE_SUCCESS) {
                    // return value
                    if (completion) {
                        // save data
                        NSDictionary *dict = (NSDictionary*) responseObject[@"data"];
                        BOOL success = NO;
                        success = [[DBManager getSharedInstance] saveUser:[Helpers parseUserObject:dict]];
                        if (success == NO) {
                            completion(FALSE);
                        }else{
                            completion(TRUE);
                        }
                    }
                }else{
                    [Helpers showAnnouncementDialog:(NSString *)responseObject[@"message"]];
                }
            });
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"%@",error);
                [MBProgressHUD hideHUDForView:currentView animated:YES];
                [Helpers showAnnouncementDialog:CONNECTION_FAILD_LABEL];
            });
        }];
    });
}
+(void)registerWithEmail:(NSString *)email password:(NSString *)password firstname:(NSString *)firstname lastname:(NSString *)lastname cb:(void (^)(BOOL))completion{
    
    UIView * currentView = [[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];
    [MBProgressHUD showHUDAddedTo:currentView animated:YES];
    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        NSString *registers = [SERVER_ADDRESS stringByAppendingString:REGISTER_WITH_EMAIL];
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        NSDictionary *params = @{@"email": email,
                                 @"password": password,
                                 @"first_name":firstname,
                                 @"last_name":lastname};
        
        [manager POST:registers parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [MBProgressHUD hideHUDForView:currentView animated:NO];
                NSLog(@"respone: %@", responseObject);
                int success = [responseObject[@"code"] intValue];
                if (success == CODE_SUCCESS) {
                    // return value
                    if (completion) {
                        completion(TRUE);
                    }
                }else{
                    [Helpers showAnnouncementDialog:(NSString *)responseObject[@"message"]];
                }
            });
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"%@",error);
                [MBProgressHUD hideHUDForView:currentView animated:YES];
                [Helpers showAnnouncementDialog:CONNECTION_FAILD_LABEL];
            });
        }];
    });
}
+ (void)registerWithFacebook:(NSString *)access_token cb:(void (^)(BOOL))completion{
    UIView * currentView = [[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];
    [MBProgressHUD showHUDAddedTo:currentView animated:YES];
    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        NSString *registersFB = [SERVER_ADDRESS stringByAppendingString:AUTH_WITH_FACEBOOK];
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        NSDictionary *params = @{@"access_token": access_token};

        [manager POST:registersFB parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [MBProgressHUD hideHUDForView:currentView animated:NO];
                NSLog(@"respone: %@", responseObject);
                int success = [responseObject[@"code"] intValue];
                if (success == CODE_SUCCESS) {
                    // return value
                    if (completion) {
                        NSDictionary *dict = (NSDictionary*) responseObject[@"data"];
                        // save data
                        BOOL success = NO;
                        success = [[DBManager getSharedInstance] saveUser:[Helpers parseUserObject:dict]];
                        if (success == NO) {
                            completion(FALSE);
                        }else{
                            completion(TRUE);
                        }

                    }
                }else{
                    [Helpers showAnnouncementDialog:(NSString *)responseObject[@"message"]];
                }
            });
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"%@",error);
                [MBProgressHUD hideHUDForView:currentView animated:YES];
                [Helpers showAnnouncementDialog:CONNECTION_FAILD_LABEL];
            });
        }];
    });
}

+ (void)registerWithPhone:(NSString *)prefix phone:(NSString *)phone cb:(void (^)(NSString*))completion{
    UIView * currentView = [[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];
    [MBProgressHUD showHUDAddedTo:currentView animated:YES];
    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        NSString *registersPhone = [SERVER_ADDRESS stringByAppendingString:AUTH_WITH_PHONE];
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        NSDictionary *params = @{@"prefix": prefix,
                                 @"phone": phone};
        
        [manager POST:registersPhone parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [MBProgressHUD hideHUDForView:currentView animated:NO];
                NSLog(@"respone: %@", responseObject);
                int success = [responseObject[@"code"] intValue];
                if (success == CODE_SUCCESS) {
                    // return value
                    if (completion) {
                        NSDictionary *dict = (NSDictionary*) responseObject[@"data"];
                        NSString *phone = dict[@"number"];
                        // save data
                        completion(phone);
                    }
                }else{
                    [Helpers showAnnouncementDialog:(NSString *)responseObject[@"message"]];
                }
            });
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"%@",error);
                [MBProgressHUD hideHUDForView:currentView animated:YES];
                [Helpers showAnnouncementDialog:CONNECTION_FAILD_LABEL];
            });
        }];
    });
}


@end
